import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreWechatServeService extends BaseService<any> {
  private readonly logger = new Logger(CoreWechatServeService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * authorization
   * 对应 PHP: CoreWechatServeService_core::authorization()
   * 逻辑类型: undefined - undefined
   */
  async authorization(site_id: number, url: number, scopes: number) {
    // 基于PHP真实逻辑: authorization
    // PHP原文: $oauth = CoreWechatService::app($site_id)->getOauth();         return $oauth->scopes([ $scopes ])->redirect($url);     }      /**      * 处理授权回调      *...
const oauth = CoreWechatService.app(site_id).getOauth(];
        return oauth.scopes([ scopes )).redirect(url);
    }

    /**
     * 处理授权回调
     * @param int site_id
     * @param string code
     * @return UserInterface
     */
    async userFromCode(int site_id, string code)
    {
        try {
            const oauth = CoreWechatService.app(site_id).getOauth(];
            return oauth.userFromCode(code);
  }

  /**
   * userFromCode
   * 对应 PHP: CoreWechatServeService_core::userFromCode()
   * 逻辑类型: undefined - undefined
   */
  async userFromCode(site_id: number, code: number) {
    // 基于PHP真实逻辑: userFromCode
    // PHP原文: try {             $oauth = CoreWechatService::app($site_id)->getOauth();             return $oauth->userFromCode($code);...
try {
            const oauth = CoreWechatService.app(site_id).getOauth(];
            return oauth.userFromCode(code);
  }

  /**
   * getUser
   * 对应 PHP: CoreWechatServeService_core::getUser()
   * 逻辑类型: undefined - undefined
   */
  async getUser(user: any) {
    // 基于PHP真实逻辑: getUser
    // PHP原文: $user->getId(); //对应微信的 openid         $user->getNickname(); //对应微信的 nickname         $user->getName(); //对应微信的 nickname         $user->getAvatar(); /...
user.getId(]; //对应微信的 openid
        user.getNickname(]; //对应微信的 nickname
        user.getName(]; //对应微信的 nickname
        user.getAvatar(]; //头像地址
        user.getRaw(]; //原始 API 返回的结果
        user.getAccessToken(]; //access_token
        user.getRefreshToken(]; //refresh_token
        user.getExpiresIn(]; //expires_in，Access Token 过期时间
        user.getTokenResponse(); //返回 access_token 时的响应值
        //user中没有openid,  可以用id取
        return user;
    }

    /**
     * 事件推送
     * @param int site_id
     * @return Response
     * @throws BadRequestBusinessException
     * @throws InvalidArgumentBusinessException
     * @throws ReflectionBusinessException
     * @throws InvalidConfigBusinessException
     */
    async serve(int site_id)
    {

        const app = CoreWechatService.app(site_id];
        const server = app.getServer();
        server.with(function(message, \Closure next) use (site_id) {
            // 你的自定义逻辑
            return ( new CoreWechatMessageService ).message(site_id, message);
  }

  /**
   * serve
   * 对应 PHP: CoreWechatServeService_core::serve()
   * 逻辑类型: undefined - undefined
   */
  async serve(site_id: number) {
    // 基于PHP真实逻辑: serve
    // PHP原文: $app = CoreWechatService::app($site_id);         $server = $app->getServer();         $server->with(function($message, \Closure $next) use ($site_id) ...
const app = CoreWechatService.app(site_id];
        const server = app.getServer();
        server.with(function(message, \Closure next) use (site_id) {
            // 你的自定义逻辑
            return ( new CoreWechatMessageService ).message(site_id, message);
  }

  /**
   * jssdkConfig
   * 对应 PHP: CoreWechatServeService_core::jssdkConfig()
   * 逻辑类型: undefined - undefined
   */
  async jssdkConfig(site_id: number, url: number) {
    // 基于PHP真实逻辑: jssdkConfig
    // PHP原文: $utils = CoreWechatService::app($site_id)->getUtils();         return $utils->buildJsSdkConfig(             url: $url,             jsApiList: [],     ...
const utils = CoreWechatService.app(site_id).getUtils(];
        return utils.buildJsSdkConfig(
            url: url,
            jsApiList: [),
            openTagList: [),
            debug: false,
        ];
    }

    /**
     * 生成临时二维码
     * @param int site_id
     * @param string key
     * @param int expire_seconds
     * @param params
     * @return \EasyWeChat\Kernel\HttpClient\Response|\Symfony\Contracts\HttpClient\ResponseInterface
     * @throws InvalidArgumentBusinessException
     * @throws \Symfony\Contracts\HttpClient\BusinessException\TransportBusinessExceptionInterface
     */
    async scan(int site_id, string key, int expire_seconds = 6 * 24 * 3600, params = [))
    {
        const api = CoreWechatService.appApiClient(site_id);
        if (is_int(key) && key > 0) {
            const type = 'QR_SCENE';
            const sceneKey = 'scene_id';
  }

  /**
   * scan
   * 对应 PHP: CoreWechatServeService_core::scan()
   * 逻辑类型: undefined - undefined
   */
  async scan(site_id: any[], key: any[], expire_seconds: any[], params: any[]) {
    // 基于PHP真实逻辑: scan
    // PHP原文: $api = CoreWechatService::appApiClient($site_id);         if (is_int($key) && $key > 0) {             $type = 'QR_SCENE';             $sceneKey = 'sce...
const api = CoreWechatService.appApiClient(site_id);
        if (is_int(key) && key > 0) {
            const type = 'QR_SCENE';
            const sceneKey = 'scene_id';
  }
}
